package com.zhousb.deviceManager.service.impl;

import com.zhousb.deviceManager.mapper.KiAssetLifecycleMapper;
import com.zhousb.deviceManager.model.dto.AssetLifecycleDto;
import com.zhousb.deviceManager.model.entity.KiAssetLifecycle;
import com.zhousb.deviceManager.model.vo.AssetLifecycleVo;
import com.zhousb.deviceManager.service.AssetLifecycleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhousb
 * @date 2024/11/5 10:15
 * @description
 */
@Service
public class AssetLifecycleServiceImpl implements AssetLifecycleService {

    @Resource
    private KiAssetLifecycleMapper kiAssetLifecycleMapper;
    @Override
    public List<AssetLifecycleVo> list(String assetId) {
        List<AssetLifecycleVo> result = new ArrayList<>();

        List<AssetLifecycleDto> dtos = kiAssetLifecycleMapper.listLifecycle(assetId);
        dtos.forEach(dto->{
            AssetLifecycleVo vo = new AssetLifecycleVo();
            vo.setEventTime(dto.getEventTime());
            vo.setContent(String.format("[%s] [%s] %s", dto.getEventType(),dto.getEventOperator(),dto.getEventContent()));
            result.add(vo);
        });
        return result;
    }

    @Override
    public boolean batchInsert(List<KiAssetLifecycle> dataList, Integer size) {
        int limit = (dataList.size() + size - 1) / size;
        List<List<KiAssetLifecycle>> splitList = Stream.iterate(0, n -> n + 1).limit(limit).parallel()
                .map(a -> dataList.stream().skip((long) a * size).limit(size).parallel()
                        .collect(Collectors.toList()))
                .toList();
        for (List<KiAssetLifecycle> list : splitList) {
            kiAssetLifecycleMapper.batchInsert(list);
        }
        return true;
    }
}
